package com.jjyd.crlp.services.impl;

import com.jjyd.crlp.domain.entity.Dictionary;
import com.jjyd.crlp.domain.entity.WordCollectionRecord;
import com.jjyd.crlp.domain.repository.DictionaryRepo;
import com.jjyd.crlp.domain.repository.WordCollectionRecordRepo;
import com.jjyd.crlp.model.code.CommonConst;
import com.jjyd.crlp.model.dto.word.SearchDto;
import com.jjyd.crlp.model.dto.word.WordDto;
import com.jjyd.crlp.services.WordService;
import com.jjyd.crlp.util.check.CheckUtil;
import com.jjyd.crlp.util.transform.ListAndStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * home界面的实现类
 */
@Service
public class WordServiceImpl implements WordService {


    @Autowired
    private DictionaryRepo dictionaryRepo;
    @Autowired
    private WordCollectionRecordRepo wordCollectionRecordRepo;

    /**
     * 通过查询内容查询单词
     *
     * @param searchDto 查询信息
     * @return 返回查询到的单词列表
     */
    @Override
    public List<WordDto> search(SearchDto searchDto) {
        String search = searchDto.getSearch();
        search = search + "%";
        List<Dictionary> dictionaries = dictionaryRepo.findByRemoveFalseAndLanguageLike(search);
        if (dictionaries == null) {
            search = "%" + search;
            dictionaries = dictionaryRepo.findByRemoveFalseAndLanguageLike(search);
        }
        //返回的list
        List<WordDto> list = new LinkedList<>();
        for (Dictionary dictionary : dictionaries) {
            WordDto wordDto = new WordDto(dictionary);
            list.add(wordDto);
        }
        return list;
    }

    /**
     * 用户收藏单词
     *
     * @param wordId 收藏的单词id
     * @param userId 用户id
     * @return 返回收藏记录id
     */
    @Override
    public Integer collect(Integer wordId, Integer userId) {
        WordCollectionRecord wordCollectionRecord = wordCollectionRecordRepo.getByRemoveFalseAndUserId(userId);
        if (wordCollectionRecord == null) {
            wordCollectionRecord = new WordCollectionRecord();
            wordCollectionRecord.setUserId(userId);
        }
        String words = wordCollectionRecord.getWords();
        List<Integer> wordIds = ListAndStringUtil.StringToList(words);
        if (!wordIds.contains(wordId)) {
            wordIds.add(wordId);
        }
        String s = ListAndStringUtil.ListToString(wordIds);
        wordCollectionRecord.setWords(s);
        Integer id = wordCollectionRecordRepo.save(wordCollectionRecord).getId();

        return id;
    }

    /**
     * 用户取消收藏单词
     *
     * @param wordId 取消收藏的单词id
     * @param userId 用户id
     * @return 返回收藏记录id
     */
    @Override
    public Integer cancelCollect(Integer wordId, Integer userId) {
        WordCollectionRecord wordCollectionRecord = wordCollectionRecordRepo.getByRemoveFalseAndUserId(userId);
        String words = wordCollectionRecord.getWords();
        List<Integer> wordIds = ListAndStringUtil.StringToList(words);
        //如果存在
        if (wordIds.contains(wordId)) {
            wordIds.remove(wordId);
        }
        String s = ListAndStringUtil.ListToString(wordIds);
        wordCollectionRecord.setWords(s);
        Integer id = wordCollectionRecordRepo.save(wordCollectionRecord).getId();
        return id;
    }

    /**
     * 查找个人所有收藏的单词
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public List<WordDto> findAll(Integer userId) {
        WordCollectionRecord wordCollectionRecord = wordCollectionRecordRepo.getByRemoveFalseAndUserId(userId);
        if (wordCollectionRecord == null) {
            wordCollectionRecord = new WordCollectionRecord();
            wordCollectionRecord.setUserId(userId);
            wordCollectionRecord.setWords("");
            wordCollectionRecordRepo.save(wordCollectionRecord);
        }
        String words = wordCollectionRecord.getWords();
        List<Integer> wordIds = ListAndStringUtil.StringToList(words);

        List<Dictionary> dictionaries = dictionaryRepo.findByRemoveFalseAndIdIn(wordIds);

        //排序，按照最早时间优先
        Map<Integer, Dictionary> map = new HashMap<>();
        for (Dictionary dictionary : dictionaries) {
            map.put(dictionary.getId(), dictionary);
        }
        List<WordDto> list = new LinkedList<>();
        for (Integer wordId : wordIds) {
            Dictionary dictionary = map.get(wordId);
            WordDto wordDto = new WordDto(dictionary);
            list.add(wordDto);
        }

        return list;
    }

    /**
     * 随机给前台返回单词
     *
     * @return 返回随机获得的单词
     */
    @Override
    public List<WordDto> random() {
        List<Dictionary> dictionaries = dictionaryRepo.findByRemoveFalse();
        CheckUtil.check("词库为空", dictionaries != null);
        List<WordDto> list = new LinkedList<>();
        if (CommonConst.WORD_Number >= dictionaries.size()) {
            for (Dictionary dictionary : dictionaries) {
                list.add(new WordDto(dictionary));
            }
        } else {
            for (int i = 0; i < CommonConst.WORD_Number; i++) {
                int temp = (int) (Math.random() * dictionaries.size());
                Dictionary remove = dictionaries.remove(temp);
                list.add(new WordDto(remove));
            }
        }

        return list;
    }
}
